import Vue from 'vue';
import App from './app.vue';
import iView, { Switch } from 'view-design';
import {router} from './router/index';
import 'famfamfam-flags/dist/sprite/famfamfam-flags.css';
import './theme.less';
import Ajax from './lib/ajax';
import Util from './lib/util';
import SignalRAspNetCoreHelper from './lib/SignalRAspNetCoreHelper';
import moment from 'moment';
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(iView);
Vue.use(VueQuillEditor);
Vue.component('ISwitch', Switch)

declare var TIMESTAMP;

import store from './store/index';
Vue.config.productionTip = false;
import { appRouters,otherRouters} from './router/router';
if(!abp.utils.getCookieValue('Abp.Localization.CultureName')){
  let language=navigator.language;
  abp.utils.setCookieValue('Abp.Localization.CultureName',language,new Date(new Date().getTime() + 5 * 365 * 86400000),abp.appPath);
}

import appconst from '@/lib/appconst';
import language from './language.json';

// Ajax.get('/AbpUserConfiguration/GetAll')
new Promise((resl, rej) => {
  let fakeData = require('@/data/AbpUserConfiguration.json')
  
  resl({
    data: fakeData
  })
})
.then((data) => {
  Util.abp=Util.extend(true,Util.abp,(data as any).data.result);
  
  // Override translations
  let dicts = abp.localization.values[appconst.localization.defaultLocalizationSourceName];
  Object.keys(abp.localization.values).forEach((key, i) => {
    if (key !== appconst.localization.defaultLocalizationSourceName) {
      Object.assign(dicts, abp.localization.values[key]);
    }
  });
  Object.assign(dicts, language);

  // set tenant to 'Default'
  Util.abp.multiTenancy.setTenantIdCookie(1);

  // init session: get user information
  store.dispatch({
    type:'session/init'
  }).then(() => {

    // FIXME: override hasPermission
    ((hasPermission) => {
      const user = (store.state as any).session.user
      Util.abp.auth.hasPermission = (permissionName) => {
        // 0是后台管理员 1是学生 2是老师
        if (!!user && user.userType === 0) {
          if (!permissionName) {
            return true;
          } else {
            return hasPermission(permissionName);
          }
        } else {
          // 不是管理员，无权限访问
        }
        return false;
      };
    })(Util.abp.auth.hasPermission);

    ((clearToken) => {
      Util.abp.auth.clearToken = () => {
        clearToken();
        abp.utils.deleteCookie('oper_id')
        abp.utils.deleteCookie('token')
      };
    })(Util.abp.auth.clearToken);

    // start vue app
    new Vue({
      render: h => h(App),
      router:router,
      store:store,
      data: {
        currentPageName: ''
      },
      async mounted () {
        this.currentPageName = this.$route.name as string;
  
        this.$store.commit('app/initCachepage');
        this.$store.commit('app/updateMenulist');
      },
      created () {
        let tagsList:Array<any> = [];
        appRouters.map((item) => {
            if (item.children.length <= 1) {
                tagsList.push(item.children[0]);
            } else {
                tagsList.push(...item.children);
            }
        });
        this.$store.commit('app/setTagsList', tagsList);
      }
    }).$mount('#app')
  })
})